//
//  TuijianPushViewController.m
//  TTMusic
//
//  Created by 马金晖 on 16/4/5.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "TuijianPushViewController.h"
#import "TuiJianPushVCCell.h"
#import "TTMusicAFNetworkTool.h"
#import "SongsListModel.h"
#import "UIImageView+WebCache.h"
#import "ButtonPushMVViewController.h"
#import "CPYSONGSModel.h"
#import "CPYDiscoveryPageSongListModel.h"

const CGFloat BackGroupHeight = 200;


@interface TuijianPushViewController ()<UITableViewDelegate
, UITableViewDataSource>

@property (nonatomic, strong) UIPopoverController *popController;


@property (nonatomic, strong)SongsListModel *songsListModel;
@property (nonatomic, strong) UIImageView *imageBG;
@property (nonatomic, strong)UIView *fenXiangView;
@property (nonatomic, strong) CPYSONGSModel *songsModel;
@end

@implementation TuijianPushViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self CreatTableView];

    [self GetDataSource];
    
}

-(void)GetDataSource {
    
    [TTMusicAFNetworkTool GETNetWithUrl:[NSString stringWithFormat:@"http://api.dongting.com/song/album/%@", self.tuiJianFirstModel.albumId] body:nil headerFile:nil response:TTMusicJSON success:^(id result) {
        
        NSDictionary *dic = result[@"data"];
        
        self.songsModel = [[CPYSONGSModel alloc] initWithDataSource:dic];
        
        self.tuijianPushModel = [[TuiJianPushModel alloc]initWithDataSource:dic];
        
        NSLog(@"111%@", self.tuijianPushModel.songList);
        [self.tableView reloadData];

        
    } failure:^(NSError *error) {
        
    }];
    

}
-(void)CreatTableView {
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SysWidth, SHORTHEIGHT) style:UITableViewStylePlain];
    
    self.tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    _tableView.contentInset = UIEdgeInsetsMake(BackGroupHeight, 0, 0, 0);
    
     self.imageBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, -BackGroupHeight, self.view.frame.size.width, BackGroupHeight)];
    
    self.fenXiangView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.bounds.size.height * 0.06)];
    self.fenXiangView.backgroundColor = [UIColor blackColor];
    self.fenXiangView.alpha = 0.8;
//    下载图片设计
    UIImageView *imageViewXia = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width * 0.1, 2, self.view.bounds.size.width * 0.1, self.view.bounds.size.width * 0.1)];
    imageViewXia.image = [UIImage imageNamed:@"xiazai.png"];
    
    [self.fenXiangView addSubview:imageViewXia];
//    收藏图片设计
    UIImageView *imageViewCang = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width * 0.45, 2, self.view.bounds.size.width * 0.1, self.view.bounds.size.width * 0.1)];
    imageViewCang.image = [UIImage imageNamed:@"shoucang1.png"];
    
    [self.fenXiangView addSubview:imageViewCang];
    
//分享图片设计
    UIImageView *imageViewfen = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width * 0.8, 2, self.view.bounds.size.width * 0.1, self.view.bounds.size.width * 0.1)];
    imageViewfen.image = [UIImage imageNamed:@"fenxiang.png"];
    
    [self.fenXiangView addSubview:imageViewfen];
    
    UIButton *fenXiangButton = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [fenXiangButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    fenXiangButton.frame = CGRectMake(self.view.bounds.size.width * 0.8, 2, self.view.bounds.size.width * 0.1, self.view.bounds.size.width * 0.1);
    
    [self.view addSubview:fenXiangButton];
    

    self.tableView.tableHeaderView = self.fenXiangView;
    
    
    
    [self.imageBG sd_setImageWithURL:[NSURL URLWithString:self.tuiJianFirstModel.picUrl]];
    
    [self.tableView addSubview:self.imageBG];
    
    [self.tableView registerClass:[TuiJianPushVCCell class] forCellReuseIdentifier:@"TuiJianPushVCCellIdentifier"];
    
    [self.view addSubview:self.tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    

    if (self.tuijianPushModel.songList.count > 0) {
        return self.tuijianPushModel.songList.count;
    }
    return 0;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TuiJianPushVCCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TuiJianPushVCCellIdentifier"];
    
    cell.songsListModel = self.tuijianPushModel.songList[indexPath.row];
    cell.NumLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];

    
    
    return cell;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return self.view.bounds.size.height * 0.1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [TTMusicAVPlayer shareTTMusicAVPlayer].local = indexPath.row;
    [[TTMusicAVPlayer shareTTMusicAVPlayer] playWithURL:_songsModel];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat yOffset = scrollView.contentOffset.y;
    CGFloat xOffset = (yOffset + BackGroupHeight) * 2;
    if (yOffset < -BackGroupHeight) {
        CGRect rect = _imageBG.frame;
        rect.origin.y = yOffset;
        rect.size.height = -yOffset;
        rect.origin.x = xOffset;
        rect.size.width = self.view.frame.size.width + fabs(xOffset) * 2;
        _imageBG.frame = rect;
        
    }
    
}


- (UIImage *)imageWithColor:(UIColor *)color {
    // 描述矩形
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    //    开启位图上下文
    UIGraphicsBeginImageContext(rect.size);
    //    获取位图上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    //    使用color演示填充上下文
    CGContextSetFillColorWithColor(context, [color CGColor]);
    //    渲染上下文
    CGContextFillRect(context, rect);
    //    从上下文中获取图片
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    //    结束上下文
    UIGraphicsEndImageContext();
    
    return theImage;
    
}


-(void)buttonAction:(UIButton *)sender {
    
       
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
